/*
 * http.h
 *
 *  Created on: Oct 7, 2013
 *      Author: killerg
 */

#ifndef HTTP_H_
#define HTTP_H_

#include <event2/http.h>
#include <event2/dns.h>
#include <event2/util.h>
#include <event2/event.h>
#include <functional>
#include <list>
#include <memory>
#include <map>
#include <string>

#include "patterns.h"


class http_server {
    public:

        struct request {
            evhttp_request* req;

            std::string ip;
            std::string path;
            std::string body;
            std::map<std::string, std::string> query;

            void send_reply(int code, const char* data);
            void send_error(int error);
        };

        typedef std::shared_ptr<http_server::request> p_request;
        typedef std::function<void(p_request req)> handle_cb_t;

        void handle(const char* path, handle_cb_t cb);

        bool init(const char* ip, int port);
        void destroy();
        void update();

    private:
        evhttp* httpd;
        event_base* base;
        std::map<std::string, handle_cb_t> hdlrs;
};

#endif /* HTTP_H_ */
